<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/5/12
 * Time: 13:30
 */
header("Content-type: text/html; charset=utf-8");
include 'helper.php';
include 'MySqlConnect.php';
$token_code = $_GET['code'];
$id = $_GET['id'];
$state = $_GET['state'];
//exit;
//echo "code=".$APPID."<br>".$APPSecret."<br>".$token_code; exit;
//通过code获取到openid

$code_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" . $APPID . "&secret=" . $APPSecret . "&code=" . $token_code . "&grant_type=authorization_code";
$code_result = curl($code_url);
$code_data = json_decode($code_result, true);
$openId = $code_data['openid'];

//获得用户相关信息
$new_access_token = $code_data['access_token'];

//$test_url = "https://api.weixin.qq.com/sns/auth?access_token=" . $new_access_token . "&openid=" . $openId;
//$test_result = curl($test_url);

//得到用户信息
$ru = "https://api.weixin.qq.com/sns/userinfo?access_token=" . $new_access_token . "&openid=" . $openId . "&lang=zh_CN";
$info_result = curl($ru);
$info_data = json_decode($info_result, true);
$headimg = $info_data['headimgurl'];//头像图片连接
$nick_name = $info_data['nickname'];//玩家姓名
$sex = $info_data['sex'];//性别
$city = $info_data['city'];//城市
//echo $openId;

//$mysqlconnect = new MySqlConnect();
//$querystr = "select * from users where openid='$openId'";
//$info =json_decode($mysqlconnect->GetDataSetJsonPHP($querystr)) ;
//$count = count($info);
//echo  $count;
//if($count=="0")
//{
//	$jieguo = $mysqlconnect->ExecuteData("insert into users values(null,'$openId', '$nick_name', '$headimg','','','',0,0,0,0,'','','','')");
//}
setcookie("openid",$openId);
setcookie("username",$nick_name);
setcookie("headimg",$headimg);
setcookie("sex",$sex);
setcookie("city",$city);
$url = "index.html";  
echo "<script language='javascript' type='text/javascript'>";  
echo "window.location.href='$url'";  
echo "</script>"; 

//$jieguo = 1;
//if($jieguo=="1")
//{
//	$url = "zhongjian.html?openid=".$openId."&id=".$id."&state=".$state;  
//	echo "<script language='javascript' type='text/javascript'>";  
//	echo "window.location.href='$url'";  
//	echo "</script>"; 
//}
//else
//{
//	//echo "报错，请联系管理员！"
//}
?>